package com.shengzhe.ostudent.utils;

import com.alibaba.fastjson.JSON;
import com.shengzhe.ostudent.common.exception.OsException;
import com.shengzhe.ostudent.utils.jdbc.JDBCUtils;
import com.shengzhe.ostudent.utils.result.E;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OsBeanUtils {


    /**
     * jdbc结果集resultSet转list
     *
     * @param resultSet 结果集
     * @param beanClass 实体类的class
     * @return 实体类对象
     */
    public static <T> List<T> resultSetToList(ResultSet resultSet, Class<T> beanClass) {
        List<T> list = new ArrayList<>();
        try {
            resultSet.last();
            int row = resultSet.getRow();
            resultSet.beforeFirst();
            for (int i = 0; i < row; i++) {
                list.add(resultSetToBean(resultSet, beanClass, row));
            }
            if (row != 1) {
                JDBCUtils.releaseResultSet(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

        return list;
    }

    /**
     * jdbc结果集resultSet转指定类型的数据
     *
     * @param resultSet 结果集
     * @return 实体类对象
     */
    public static Long resultSetToLong(ResultSet resultSet) {
        try {
            resultSet.next();
            return resultSet.getLong(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取这个类和他父类的所有私有字段
     *
     * @param clazz 类
     */
    public static List<Field> getPrivateFields(Class<?> clazz) {
        List<Field> fields = new ArrayList<>();

        while (clazz != null) {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (Modifier.isPrivate(declaredField.getModifiers())) {
                    fields.add(declaredField);
                }
            }
            clazz = clazz.getSuperclass();
        }

        return fields;
    }

    /**
     * jdbc结果集resultSet转Java实体类
     *
     * @param resultSet 结果集
     * @param beanClass 实体类的class
     * @return 实体类对象
     */
    public static <T> T resultSetToBean(ResultSet resultSet, Class<T> beanClass) {
        return resultSetToBean(resultSet, beanClass, 1);
    }

    /**
     * jdbc结果集resultSet转Java实体类
     *
     * @param resultSet 结果集
     * @param beanClass 实体类的class
     * @return 实体类对象
     */
    public static <T> T resultSetToBean(ResultSet resultSet, Class<T> beanClass, int row) {
        try {
            resultSet.next();
            T bean = beanClass.newInstance();
            for (Field field : getPrivateFields(beanClass)) {
                String paramName = field.getName();
                String paramValue = resultSet.getString(paramName);
                if (paramValue != null && !paramValue.isEmpty()) {
                    field.setAccessible(true);
                    Class<?> type = field.getType();
                    field.set(bean, parseValue(type, paramValue));
                }
            }
            if (row == 1) {
                JDBCUtils.releaseResultSet(resultSet);
            }
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 参数类型转换
     *
     * @param type       实体类中的参数类型
     * @param paramValue 类型未知的参数数据
     * @return 转换完毕后的参数
     */
    public static Object parseValue(Class<?> type, String paramValue) {
        Object value = null;
        if (type == String.class) {
            value = paramValue;
        } else if (type == int.class || type == Integer.class) {
            value = Integer.parseInt(paramValue);
        } else if (type == long.class || type == Long.class) {
            value = Long.parseLong(paramValue);
        } else if (type == double.class || type == Double.class) {
            value = Double.parseDouble(paramValue);
        } else if (type == float.class || type == Float.class) {
            value = Float.parseFloat(paramValue);
        } else if (type == boolean.class || type == Boolean.class) {
            value = Boolean.parseBoolean(paramValue);
        } else if (type == byte.class || type == Byte.class) {
            value = Byte.parseByte(paramValue);
        } else if (type == short.class || type == Short.class) {
            value = Short.parseShort(paramValue);
        } else if (type == char.class || type == Character.class) {
            value = paramValue.charAt(0);
        } else if (type == Date.class) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                value = simpleDateFormat.parse(paramValue);
            } catch (ParseException e) {
                e.printStackTrace();
                throw new OsException(E.REQUEST_CODE_N9);
            }
        }
        return value;
    }

    /**
     * 复制bean到一个新对象
     *
     * @param orig 原始bean
     * @param t    新对象的class
     * @return 新的对象
     */
    public static <T> T copyBean(Object orig, Class<T> t) {
        return JSON.parseObject(JSON.toJSONString(orig), t);
    }
}
